package com.fencing_squad.homo.util;

import com.bumptech.glide.Glide;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.list.Offer;
import com.fencing_squad.homo.list.Product;
import com.fencing_squad.homo.util.function.FunctionInt;
import com.fencing_squad.homo.util.function.FunctionString;
import com.fencing_squad.homo.util.function.FunctionVoid;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.media.image.PixelMap;

public class CustomComponent {
    public static CommonDialog getDialog(Context context, String message, FunctionVoid confirm, FunctionVoid cancel) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog, null, false);
        Component dialogWindow = component.findComponentById(ResourceTable.Id_dialogWindow);
        Text dialogMessage = (Text)component.findComponentById(ResourceTable.Id_dialogMessage);
        dialogMessage.setText(message);
        Text okText = (Text)component.findComponentById(ResourceTable.Id_okText);
        Text cancelText = (Text)component.findComponentById(ResourceTable.Id_cancelText);
        AnimatorProperty showWindow = AnimationConfig.getDialogShowAnim(dialogWindow);

        CommonDialog dialog = new CommonDialog(context) {
            @Override
            protected void onHide() {
                super.onHide();
                destroy();
            }

            @Override
            protected void onShowing() {
                super.onShowing();
                showWindow.start();
            }
        };

        okText.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (confirm != null) {
                    confirm.func();
                }
                else if (dialog != null) {
                    dialog.hide();
                }
            }
        });

        cancelText.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (cancel != null) {
                    cancel.func();
                }
                else if (dialog != null) {
                    dialog.hide();
                }
            }
        });

        dialog.setContentCustomComponent(component);
        dialog.setSize(Screen.getScreenWidth(context), Screen.getScreenHeight(context));
        dialog.setTransparent(true);
        return dialog;
    }

    public static void setDialog(CommonDialog dialog, String message, FunctionVoid confirm, FunctionVoid cancel) {
        Component component = dialog.getContentCustomComponent();
        Text dialogMessage = (Text)component.findComponentById(ResourceTable.Id_dialogMessage);
        dialogMessage.setText(message);
        dialog.setContentCustomComponent(component);
    }

    public static CommonDialog getOfferDetail(Context context, FunctionString submit, Offer offer) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_offer_detail, null, false);
        Component window = component.findComponentById(ResourceTable.Id_window);
        Component close = component.findComponentById(ResourceTable.Id_close);
        Component confirm = component.findComponentById(ResourceTable.Id_submit);
        Text content = (Text) component.findComponentById(ResourceTable.Id_content);
        Image img = (Image) component.findComponentById(ResourceTable.Id_image);
        Text toDetail = (Text) component.findComponentById(ResourceTable.Id_toDetail);
        Text fromDetail = (Text) component.findComponentById(ResourceTable.Id_fromDetail);
        Text type = (Text) component.findComponentById(ResourceTable.Id_type);
        Text level = (Text) component.findComponentById(ResourceTable.Id_level);
        Text reward = (Text) component.findComponentById(ResourceTable.Id_reward);
        AnimatorProperty showWindow = AnimationConfig.getDialogShowAnim(window);

        content.setText(offer.getContent());
        toDetail.setText(offer.getToDetail());
        fromDetail.setText(offer.getFromDetail());
        type.setText(offer.getType());
        level.setText(offer.getLevel());
        reward.setText(offer.getReward());
        if (!offer.getImg().equals("")) {
            img.setVisibility(Component.VISIBLE);
            Glide.with(context).load(offer.getImg()).listener(ImageLoader.fail(img, ResourceTable.Media_icon)).into(img);
        }

        CommonDialog dialog = new CommonDialog(context) {
            @Override
            protected void onHide() {
                super.onHide();
                destroy();
            }

            @Override
            protected void onShowing() {
                super.onShowing();
                showWindow.start();
            }
        };

        close.setClickedListener(component1 -> {
            if (dialog != null) {
                dialog.hide();
            }
        });

        confirm.setClickedListener(component1 -> {
            if (submit != null) {
                submit.func(offer.getRewardId());
            }
        });
        
        dialog.setContentCustomComponent(component);
        dialog.setSize(Screen.getScreenWidth(context), Screen.getScreenHeight(context));
        dialog.setTransparent(true);
        return dialog;
    }

    public static CommonDialog getNewPost(Context context, int type, FunctionString textFunc, FunctionInt imageFunc, FunctionVoid submitFunc, FunctionVoid clearFunc) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_post_submit, null, false);
        Component window = component.findComponentById(ResourceTable.Id_window);
        Text title = (Text) component.findComponentById(ResourceTable.Id_title);
        TextField postText = (TextField) component.findComponentById(ResourceTable.Id_postText);
        Component selectImg = component.findComponentById(ResourceTable.Id_addPhoto);
        Button submit = (Button) component.findComponentById(ResourceTable.Id_submit);
        Component close = component.findComponentById(ResourceTable.Id_close);
        AnimatorProperty showWindow = AnimationConfig.getDialogShowAnim(window);
        if (type == 1) {
            title.setText("编辑悬赏内容");
            submit.setText("确定");
            postText.setHint("输入悬赏内容");
        }

        selectImg.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (type == 0) {
                    imageFunc.func(200);
                }
                else if (type == 1) {
                    imageFunc.func(201);
                }
            }
        });

        postText.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                textFunc.func(s);
            }
        });

        CommonDialog dialog = new CommonDialog(context) {
            @Override
            protected void onHide() {
                super.onHide();
                destroy();
            }

            @Override
            protected void onShowing() {
                super.onShowing();
                showWindow.start();
            }
        };

        close.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dialog.hide();
                if (clearFunc != null) {
                    clearFunc.func();
                }
            }
        });

        submit.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (submitFunc != null) {
                    submitFunc.func();
                }
                else {
                    dialog.hide();
                }
            }
        });

        dialog.setContentCustomComponent(component);
        dialog.setSize(Screen.getScreenWidth(context), Screen.getScreenHeight(context));
        dialog.setTransparent(true);
        return dialog;
    }

    public static void addImage(CommonDialog dialog, PixelMap pixelMap) {
        Component component = dialog.getContentCustomComponent();
        TableLayout layout = (TableLayout) component.findComponentById(ResourceTable.Id_photo);
        Image image = new Image(component.getContext());
        image.setWidth(240);
        image.setHeight(240);
        image.setMarginRight(60);
        image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
        image.setClipAlignment(Image.CLIP_DIRECTION_NOT_SET);
        image.setPixelMap(pixelMap);
        layout.addComponent(image, 0);
        System.out.println("finished");
    }

    public static CommonDialog getNewPost(Context context, FunctionString textFunc, FunctionInt imageFunc, FunctionVoid submitFunc, FunctionVoid clearFunc) {
        return getNewPost(context, 0, textFunc, imageFunc, submitFunc, clearFunc);
    }

    public static CommonDialog getOrder(Context context, Product product, FunctionString setName, FunctionString setPhone, FunctionString setAddress, FunctionString setAddressDetail, FunctionVoid confirm) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_order, null, false);
        Component window = component.findComponentById(ResourceTable.Id_window);
        AnimatorProperty showWindow = AnimationConfig.getDialogShowAnim(window);
        Text title = (Text) component.findComponentById(ResourceTable.Id_title);
        Text number = (Text) component.findComponentById(ResourceTable.Id_number);
        Text sum = (Text) component.findComponentById(ResourceTable.Id_sum);
        TextField name = (TextField) component.findComponentById(ResourceTable.Id_name);
        TextField phone = (TextField) component.findComponentById(ResourceTable.Id_phone);
        TextField address = (TextField) component.findComponentById(ResourceTable.Id_address);
        TextField addressDetail = (TextField) component.findComponentById(ResourceTable.Id_addressDetail);
        Component submit = component.findComponentById(ResourceTable.Id_submit);
        Component close = component.findComponentById(ResourceTable.Id_close);
        Image image = (Image) component.findComponentById(ResourceTable.Id_image);

        CommonDialog dialog = new CommonDialog(context) {
            @Override
            protected void onHide() {
                super.onHide();
                destroy();
            }

            @Override
            protected void onShowing() {
                super.onShowing();
                showWindow.start();
            }
        };

        close.setClickedListener(component1 -> {
            dialog.hide();
        });
        title.setText(product.getName());
        number.setText(product.getNumber());
        sum.setText(String.valueOf(product.getNumberInt() * product.getPointInt()));
        submit.setClickedListener(component1 -> {
            confirm.func();
        });

        name.addTextObserver((s, i, i1, i2) -> {
            setName.func(s);
        });

        phone.addTextObserver((s, i, i1, i2) -> {
            setPhone.func(s);
        });

        address.addTextObserver((s, i, i1, i2) -> {
            setAddress.func(s);
        });

        addressDetail.addTextObserver((s, i, i1, i2) -> {
            setAddressDetail.func(s);
        });

        Glide.with(context).load(Network.getBaseURL() + product.getImage()).listener(ImageLoader.fail(image, ResourceTable.Media_icon)).into(image);

        dialog.setContentCustomComponent(component);
        dialog.setSize(Screen.getScreenWidth(context), Screen.getScreenHeight(context));
        dialog.setTransparent(true);
        return dialog;
    }

    public static ToastDialog getToastDialog(Context context, String message) {
        ToastDialog dialog = new ToastDialog(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_toast, null, false);
        Text msg = (Text) component.findComponentById(ResourceTable.Id_message);
        msg.setText(message);
        dialog.setContentCustomComponent(component);
        dialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        dialog.setAlignment(LayoutAlignment.BOTTOM|LayoutAlignment.HORIZONTAL_CENTER);
        dialog.setTransparent(true);
        return dialog;
    }
}
